class Restaurants::PrintersController <  Restaurants::RestaurantsController
  before_action :get_printer,only:[:edit,:show,:update,:destroy]

  def index
    @nav_title = "打印机管理"
    add_breadcrumb(create_nav @nav_title,'#')
    @printers = @restaurant.printers
  end

  def new
    @printer = Printer.new
    set_new_breadcrumb
  end

  def edit
    set_edit_breadcrumb
  end  

  def create
    @printer = Printer.new(printers_params)
    @printer.restaurant_id = @restaurant.id
    if @printer.save
      redirect_to action: :index
    else
      set_new_breadcrumb
      render :new
    end  
  end

  def update
    if @printer.update_attributes(printers_params)
      redirect_to action: :index
    else
      set_edit_breadcrumb
      render :edit
    end  
  end

  def destroy
    @printer.destroy
    redirect_to action: :index, restaurant_id: @printer.restaurant_id
  end




  private
 
  def set_new_breadcrumb
       @nav_title = "新建打印机"
       add_breadcrumb create_nav('打印机管理','/restaurants/printers')
       add_breadcrumb create_nav('新建打印机', '/restaurants/printers/new')
   end 

   def set_edit_breadcrumb
       @nav_title = "修改#{@printer.name}"
       add_breadcrumb(create_nav '打印机管理','/restaurants/printers') 
       add_breadcrumb(create_nav @nav_title,"#")
   end

  def get_printer
    @printer = Printer.find params[:id]
  end  

  def printers_params
    params.require(:printer).permit(:key,:name,:sn,:restaurant_id)
  end 

end
